package site.user.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyBase;
import modules.recruit.entity.ApplyConfirm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.utils.CommonUtils;
import com.guinea.core.web.StrutsSimpleAction;

import ex.recruit.service.ApplyBaseServiceEx;

public abstract class ApplyCommonStep extends StrutsSimpleAction {
	public static final String LIST = "list";

	public static final String EDIT = "edit";

	protected ApplyBaseServiceEx applyBaseServiceEx;

	protected Long applyBaseId;

	protected ApplyBase applyBase;

	public ApplyBaseServiceEx getApplyBaseServiceEx() {
		return applyBaseServiceEx;
	}

	public void setApplyBaseServiceEx(ApplyBaseServiceEx applyBaseServiceEx) {
		this.applyBaseServiceEx = applyBaseServiceEx;
	}

	protected void initProperties() {
		super.initProperties();
		applyBaseId = null;
		applyBase = null;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form, request);
		applyBaseId = (Long) getEntityId(request, "applyBaseId", Long.class);
		if (applyBaseId != null) {
			applyBase = getApplyBaseServiceEx().getApplyBase(applyBaseId);
		}
	}

	protected boolean validateApplyBase() {
		if (applyBase == null) {
			return valid = false;
		}
		return true;
	}
	
	/**
	 * url参数未定义method时的默认Action函数. 默认为index Action.
	 */
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward(LIST);
	}
	
	/**
	 * edit验证.
	 */
	public ActionForward editValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form, request);
		if (!validateApplyBase())
			return handleEntityMissing(mapping, form, request, response);
		
		if(!CommonUtils.couldApplyBeModified(applyBase)){
			valid = false;
			saveDirectlyError(request, "该申请已提交，不能再修改。");
			return mapping.findForward(LIST);
		}

		return null;
	}

	/**
	 * 保存前的验证.
	 */
	public ActionForward saveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (isCancelled(request)){
			valid = false;
			return mapping.findForward(LIST);
		}
		if (!validateToken(request))
			return handleToken(mapping, form, request, response);

		initFirst(form, request);
		if (!validateApplyBase())
			return handleEntityMissing(mapping, form, request, response);

//		if (!validateForm(mapping, form, request))
//			return handleFormErrors(mapping, form, request, response);
		return null;
	}

	protected ActionForward handleEntityMissing(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		saveError(request, "entity.missing");
		return mapping.findForward(LIST);
	}

	protected ActionForward handleToken(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		saveError(request, "submit.duplicated");
		return handleEdit(mapping, form, request, response);
	}

	protected ActionForward handleFormErrors(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		saveErrors(request, formErrors);
		return handleEdit(mapping, form, request, response);
	}

	protected ActionForward handleEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		saveToken(request);
		request.setAttribute("applyBase",applyBase);
		return mapping.findForward(EDIT);
	}

}
